package com.pospi.pai.yunpos.util;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.lany.sp.SPHelper;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.greendao.LogBeenDao;
import com.pospi.http.HttpConnection;
import com.pospi.pai.yunpos.been.LogBeen;
import com.pospi.pai.yunpos.login.Constant;
import com.pospi.util.App;
import com.pospi.util.GetData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by acer on 2018/9/8.
 */

public class LogUtil {
    public void save(String type) {
        LogBeen been = new LogBeen();
        been.setStatus("0");
        been.setMarket(SPHelper.getInstance().getString(Constant.STORE_ID));
        been.setOptdt(GetData.getDataTime());
        been.setSyjh(SPHelper.getInstance().getString(Constant.SYJH));
        been.setType(type);
        been.setUserid(SPHelper.getInstance().getString(Constant.CUSTOMER));
        been.setUsername(SPHelper.getInstance().getString(Constant.CUSTOMER_name));
        LogBeenDao dao = App.getInstance().getDaoSession().getLogBeenDao();
        dao.insert(been);
    }

    public List<LogBeen> searchAll() {
        LogBeenDao dao = App.getInstance().getDaoSession().getLogBeenDao();
        return dao.queryBuilder().list();
    }

    public List<LogBeen> searchNoUp() {
        LogBeenDao dao = App.getInstance().getDaoSession().getLogBeenDao();
        return dao.queryBuilder().where(LogBeenDao.Properties.Status.eq("0")).list();
    }

    public void updateSatutas(List<LogBeen> beens) {
        LogBeenDao dao = App.getInstance().getDaoSession().getLogBeenDao();
        for (LogBeen been : beens) {
            been.setStatus("1");
        }
        dao.updateInTx(beens);
    }

    public void upData(Context context, final List<LogBeen> beens) {
        Map<String, String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "pos.poslog");
        params.put("fun", "upload_poslog");
        params.put("pds", getJson(beens));//
        Log.i("result_up", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(final String Response) {
                Log.i("result_up", Response);
                try {
                    JSONObject object = new JSONObject(Response);
                    String code = object.getString("errCode");
                    String msg = object.getString("errMsg");
                    if (code.equals("100")) {
                        updateSatutas(beens);
                    }
                    Log.i("msg", msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private String getJson(List<LogBeen> list) {
        JSONObject object = new JSONObject();
        try {
            JSONArray array = new JSONArray();
            for (LogBeen been : list) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("market", been.getMarket());
                jsonObject.put("optdt", been.getOptdt());
                jsonObject.put("syjh", been.getSyjh());
                jsonObject.put("type", been.getType());
                jsonObject.put("userid", been.getUserid());
                jsonObject.put("username", been.getUsername());
                array.put(jsonObject);
            }
            object.put("log", array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object.toString();
    }

}
